package com.example.onlinemallsystem.controller;

import com.example.onlinemallsystem.entity.Category;
import com.example.onlinemallsystem.service.CategoryService;
import com.example.onlinemallsystem.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/Categories")
@CrossOrigin(origins = "http://localhost:5173", allowCredentials = "true")
@Validated
public class CategoryController extends BaseController {

    @Autowired
    private CategoryService categoryService;

    // 查询所有商品分类
    @GetMapping("/selectAll")
    public JsonResult<List<Category>> getAllCategories() {
        List<Category> categories = categoryService.getAllCategories();
        return  new JsonResult<>(200,categories); // 返回数据和成功状态
    }

    // 根据id获取分类详情
    @GetMapping("/{id}")
    public JsonResult<Category> getCategoryById(@PathVariable Long id) {
        Category category = categoryService.getCategoryById(id);
        return  new JsonResult<>(200,category); // // 返回数据和成功状态
    }

    // 获取指定父分类下的子分类
    @GetMapping("/parent/{parentId}")
    public JsonResult<List<Category>> getCategoriesByParentId(@PathVariable Long parentId) {
        List<Category> categories = categoryService.getCategoriesByParentId(parentId);
        return  new JsonResult<>(200,categories); // 返回数据和成功状态
    }

    @PostMapping("/add")
    public JsonResult<Long> addCategory(@RequestBody @Validated Category category) {
        // 校验 parent_id 和 level 是否存在
        if (category.getParentId() == null) {
            return  new JsonResult<>(400,"添加分类时必须指定 parent_id");
        }

        if (category.getLevel() == null) {
            return  new JsonResult<>(401,"添加分类时必须指定 level");
        }

        // 校验 level 和 parent_id 的逻辑关系
        if (category.getLevel() == 1 && !category.getParentId().equals(0L)) {
            return  new JsonResult<>(402,"一级分类的 parent_id 必须为 0");
        }

        if (category.getLevel() > 1 && category.getParentId() == 0L) {
            return  new JsonResult<>(403,"非一级分类的 parent_id 不能为 0");
        }

        return  new JsonResult<>(200,"分类添加成功");
    }

    @PostMapping("/seller/add")
    public JsonResult<Category> addSellerCategory(@RequestBody Category category, HttpSession session) {
        Long sellerId = (Long) session.getAttribute("userId");
        if (sellerId == null) {
            return new JsonResult<>(401, "用户未登录");
        }
        category.setSellerId(sellerId);
        int result = categoryService.insertCategory(category);
        if (result > 0) {
            return new JsonResult<>(200, "添加成功", category);
        }
        return new JsonResult<>(400, "添加失败");
    }


    // 更新商品信息
    @PutMapping("/update/{id}")
    public JsonResult<Boolean> updateCategory(@PathVariable Long id, @Validated Category category ) {
        // 验证 ID 是否一致
        if (!id.equals(category.getId())) {
            return new JsonResult<>(400, "路径参数 ID 与请求体 ID 不一致");
        }

        boolean success = categoryService.updateCategory(category);

        if (success) {
            return new JsonResult<>(200, "分类更新成功");
        } else {
            return new JsonResult<>(400, "分类更新失败，请检查参数或联系管理员");
        }
    }

    // 删除分类
    @DeleteMapping("/{id}")
    public JsonResult<Boolean> deleteCategory(@PathVariable Long id) {

        boolean success = categoryService.deleteCategory(id);
        if (success) {
            return new JsonResult<>(200, "分类删除成功");
        } else {
            return new JsonResult<>(400, "分类删除失败，请检查参数或联系管理员");
        }

    }
    @PostMapping("/test")
    public String test() {
        return "success";
    }
}